package org.song.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.song.annotation.LogRecord;
import org.song.annotation.RequestTimeRecord;
import org.song.exception.result.AjaxResult;
import org.song.model.dto.MessageDto;
import org.song.model.vo.Message;
import org.song.service.MessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 博客留言控制层
 */
@Api(tags = "博客留言控制层")
@Controller
@RequestMapping(value = "/message")
public class MessageController {

    @Autowired( required = true )
    @Qualifier( value = "messageServiceImpl")
    private MessageService messageService;

    /**
     * 查询所有的博客留言
     * @param messageDto
     * @return
     */
    @RequestTimeRecord
    @ApiOperation(value = "查询所有的博客留言" , notes = "查询所有的博客留言")
    @ResponseBody
    @RequestMapping(value = "/listMessages" , method = RequestMethod.GET)
    public AjaxResult<PageInfo<Message>> listMessages(@ApiParam(value = "messageDto实例接受查询参数") @NonNull MessageDto messageDto) {
        PageHelper.startPage(messageDto.getPage() , messageDto.getSize());
       return AjaxResult.success(new PageInfo<>( messageService.listMessages(messageDto) ));
    }

    /**
     * 删除博客留言
     * @param message
     * @return
     */
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "删除博客留言" , notes = "删除博客留言")
    @ResponseBody
    @RequestMapping(value = "/deleteMessage" , method = RequestMethod.DELETE)
    public AjaxResult<Integer> deleteMessage(@ApiParam(value = "接受需要删除的博客留言") @NonNull @RequestBody Message message) {
        return AjaxResult.success(messageService.deleteMessage(message));
    }

    /**
     * 增加博客留言
     * @param message
     * @return
     */
    @RequestTimeRecord
    @LogRecord
    @ApiOperation(value = "增加博客留言" , notes = "增加博客留言")
    @ResponseBody
    @RequestMapping(value = "/insertMessage" , method = RequestMethod.PUT)
    public AjaxResult<Integer> insertMessage(@ApiParam(value = "博客留言") @NonNull @RequestBody Message message) {
        return AjaxResult.success(messageService.insertMessage(message));
    }

    /**
     * 更新博客留言
     * @param message
     * @return
     */
    @LogRecord
    @ApiOperation(value = "更新博客留言" , notes = "更新博客留言")
    @ResponseBody
    @RequestMapping(value = "/updateMessage" , method = RequestMethod.POST)
    public AjaxResult<Integer> updateMessage(@ApiParam(value = "根据传入的留言信息进行博客留言更新") @NonNull @RequestBody Message message) {
        return AjaxResult.success(messageService.updateMessage(message));
    }


}
